//
//  CarConditionScrollView.m
//  carcareIOS
//
//  Created by wr on 16/3/1.
//  Copyright © 2016年 baozun. All rights reserved.
//

#import "CarConditionScrollView.h"
#import "UmengMgr.h"

typedef NS_ENUM(NSInteger , VIEW_TYPE) {
//    VIEW_TYPE_BACKVIEW = 10,
    VIEW_TYPE_DETAILLABEL = 20,
    VIEW_TYPE_LABLE = 66,
};

@interface CarConditionScrollView()

@property (nonatomic , strong) UIScrollView * bodyScrollView;

@property (nonatomic , strong) UIPageControl * numPageControl;

//页数
@property (nonatomic , assign) NSInteger pageCountNum;

@property (nonatomic , copy) NSArray * dataArr;

@end

@implementation CarConditionScrollView

-(void)dealloc{
    [self.bodyScrollView removeObserver:self forKeyPath:@"contentOffset"];
}

- (instancetype)initWithFrame:(CGRect)frame withDataArray:(NSArray *)dataArray
{
    self = [super initWithFrame:frame];
    if(self)
    {
        self.backgroundColor = [UIColor whiteColor];
//        self.layer.borderWidth = 0.5;
//        self.layer.borderColor = COLOR_HEXSTRING(@"#D6D6D3").CGColor;
        self.pageCountNum = dataArray.count / 4 + (dataArray.count % 4 == 0 ? 0 : 1);
        self.dataArr = dataArray;

        [self createScrollView];
        [self createPageControl];
        
        [self.bodyScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
        
    }
    return self;
}
- (void)createPageControl
{
    self.numPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.frame.size.height - 15, self.frame.size.width, 10)];
    self.numPageControl.backgroundColor = [UIColor clearColor];
    self.numPageControl.numberOfPages = self.pageCountNum;
    self.numPageControl.currentPageIndicatorTintColor = COLOR_HEXSTRING(@"#A3A3A3");
    self.numPageControl.pageIndicatorTintColor = [UIColor colorWithPatternImage:ImageNamed(@"pageControllCicle")];

    self.numPageControl.enabled = NO;
    [self addSubview:self.numPageControl];
    
    self.numPageControl.hidden = (self.pageCountNum > 1 ? NO : YES);
}
- (void)createScrollView
{
    self.bodyScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
    self.bodyScrollView.backgroundColor = [UIColor clearColor];
    self.bodyScrollView.pagingEnabled = YES;
    self.bodyScrollView.showsHorizontalScrollIndicator = NO;
    self.bodyScrollView.showsVerticalScrollIndicator = NO;
    [self.bodyScrollView setContentSize:CGSizeMake(self.frame.size.width * self.pageCountNum, self.frame.size.height)];
    [self addSubview:self.bodyScrollView];
    
    for (int index = 0 ; index < self.pageCountNum; index ++) {
        UIView * bigBackView = [[[UIView viewWithFrame:CGRectMake(self.bodyScrollView.width * index, 0, self.bodyScrollView.width, self.bodyScrollView.height)] resetBackgroundColor:[UIColor clearColor]] setupOnView:self.bodyScrollView];
        
        for (int smallIndex = index * 4 ; smallIndex < (self.dataArr.count < (index + 1) * 4 ? self.dataArr.count : (index + 1) * 4); smallIndex ++)
            {
                int nowIndex = smallIndex - 4 * index;
                CGFloat smallViewW = bigBackView.width / 2;
                CGFloat smallViewH = (bigBackView.height - 15) / 2;
                [self loadViewWithSuperView:bigBackView withFrame:CGRectMake(nowIndex % 2 *smallViewW , (nowIndex > 1 ? smallViewH : 0), smallViewW, smallViewH) withTitle:[self.dataArr[smallIndex] firstObject] withImageName:self.dataArr[smallIndex][1] withIndexNum:[self.dataArr[smallIndex][2]intValue]];
            }
    }
}

-(UIView *)loadViewWithSuperView:(UIView *)superView withFrame:(CGRect)frame withTitle:(NSString * )titleStr withImageName:(NSString *)imageStr withIndexNum:(int)indexNum
{
    UIView * backView  = [[[UIView viewWithFrame:frame] resetBackgroundColor:[UIColor clearColor]] setupOnView:superView];
//    backView.tag = VIEW_TYPE_BACKVIEW + indexNum;
    CGFloat iconImageVX = 29 * __MAIN_RATIO_375;

    CGFloat iconImageVW = ImageNamed(imageStr).size.width;
    CGFloat iconImageVY = (frame.size.height - iconImageVW) / 2;
    [Factory imageViewWithFrame:CGRectMake(iconImageVX,iconImageVY , iconImageVW, iconImageVW) image:[UIImage imageNamed:imageStr] onView:backView];

    CGFloat titleLabelX = iconImageVX + iconImageVW + 9 * __MAIN_RATIO_375;
    CGFloat titleLabelY = iconImageVY + (iconImageVW - 20)/ 2;
    UILabel * titleLabel = [Factory labelWithFrame:CGRectMake(titleLabelX, titleLabelY, 70, 20) font:FONT_DEFAULT_Light(15 * __MAIN_RATIO_375) text:titleStr textColor:[Theme wordColor] onView:backView textAlignment:NSTextAlignmentLeft];
    titleLabel.tag = VIEW_TYPE_LABLE + indexNum;

    UILabel * detailTitleLabel = [Factory labelWithFrame:CGRectMake(titleLabelX, titleLabel.bottom - (iconImageVW - 20)/ 2 , backView.width - titleLabelX, 20) font:[UIFont systemFontOfSize:12] text:@"" textColor:[Theme wordGrayNewColor] onView:backView textAlignment:NSTextAlignmentLeft];
    detailTitleLabel.tag = VIEW_TYPE_DETAILLABEL + indexNum;

    UIButton * backButton = [ViewFactory buttonWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) title:nil normalColor:[UIColor clearColor] highlightColor:[[UIColor blackColor] colorWithAlphaComponent:0.1]  disabledColor:[[UIColor blackColor] colorWithAlphaComponent:0.1] inView:backView action:nil];
    backButton.tag = indexNum;
    backButton.layer.cornerRadius = 0;
    backButton.layer.masksToBounds = NO;
    [backButton addTarget:self action:@selector(receiveButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    return backView;
}
- (void)receiveButtonClick:(UIButton *)sender
{
    if (self.funcButtonClick) self.funcButtonClick(sender);
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
//    self.nowImageCountInt = self.bodyScrollView.contentOffset.x / self.bodyScrollView.width;
    NSInteger lastPage = self.numPageControl.currentPage;
    self.numPageControl.currentPage = self.bodyScrollView.contentOffset.x / __MAIN_WIDTH;
    if (lastPage != self.numPageControl.currentPage) {
        [UmengMgr event:ICZ_Function_Slide];
    }
    
}
- (void)reloadDetailLabelText:(NSString *)detailText withTagNum:(NSInteger)tagNum
{
    //改变titlelabel 位置
    UILabel * titleLabel = (UILabel *)[self viewWithTag:VIEW_TYPE_LABLE + tagNum];
    UILabel * detailLabel = (UILabel *)[self viewWithTag:VIEW_TYPE_DETAILLABEL + tagNum];
    [titleLabel setTop:(detailLabel.top - titleLabel.height)];
    detailLabel.text = detailText;
}
@end
